/*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* Alternatively, this software may be distributed under the terms of BSD
* license.
*
*
*
* Authors:                                zhoujian
* Email:        343600434@qq.com
* Date:        Jan-09-2024 15:42:24
*
*/


#ifndef __led_H__
#define __led_H__

#include "FreeRTOS.h"

#include "task.h"



#ifdef __cplusplus
extern "C" {
#endif



#define FROM_1_TO_8_DELAY 2

extern uint16_t led_array[4][4];
extern uint8_t update_led;

#if 0

#define LED_ON(N) led_array[(N-1)%8][(((N-1)/8>15)?1:0)] |= (1<<((((N-1)/8)>15)?((N-1)/8-16):((N-1)/8)))
#define LED_OFF(N) led_array[(N-1)%8][(((N-1)/8>15)?1:0)] &= ~(1<<((((N-1)/8)>15)?((N-1)/8-16):((N-1)/8)))
#define LED_ON(N)   do{ \
                        if(N>256){  \
                            led_array[(N-257)/7][4] |= (1<<((N-257)%7)); \
                        }else{      \
                            led_array[(N-1)%64/16][(N-1)/64] |= (1<<((N-1)%16)); \
                        }           \
                    }while(0)
#define LED_OFF(N)  do{ \
                        if(N>256){  \
                            led_array[(N-257)/7][4] &= ~(1<<((N-257)%7)); \
                        }else{      \
                            led_array[(N-1)%64/16][(N-1)/64] &= ~(1<<((N-1)%16)); \
                        }           \
                    }while(0)
#else

#define LED_ON(N)   do{ \
                                            led_array[(N-1)%64/16][(N-1)/64] |= (1<<((N-1)%16)); \
                    }while(0)


#define LED_OFF(N)  do{ \
                                            led_array[(N-1)%64/16][(N-1)/64] &= ~(1<<((N-1)%16)); \
                    }while(0)



#endif

void led_delay(void);
void led_init(void);
void led_task(void);
void boot_update_speed_led(uint16_t speed_value);
uint32_t get_dispaly_value(uint32_t v);
void update_speed_to_led();
void compute_speed_value(uint16_t speed_value);

void update_rate_led(void);
void boot_led_flush(void);
void toggle_mph_display(void);

void update_mph_led(void);
void toggle_trip_odo(void);
uint8_t is_odo(void);
uint8_t is_mph(void);

void update_trip_led(void);


extern volatile uint8_t dis_mph;
extern uint8_t trip_odo;

extern volatile uint16_t delay_running;
//#define MONI_SPI0


#define PWM_LED_OE


//#define DEBUG_PWM_LED_ALWAYS_ON
//#define DEBUG_BOOT_ANIM
//#define DEBUG_PWM_LED_VALUE

#ifndef PWM_LED_OE

#define HIGH_V 0x3FF//0xC0
#define LOW_V 20


#endif

/* chip select invalid */
#define SPI_SM16306S_LE_HIGH  do{GPIO_BOP(GPIOA) = GPIO_PIN_15;}while(0)
//gpio_bit_set(GPIOD, GPIO_PIN_2)

#define SPI_SM16306S_LE_LOW do{GPIO_BC(GPIOA) = GPIO_PIN_15;}while(0)
//gpio_bit_reset(GPIOD, GPIO_PIN_2)

#ifdef PWM_LED_OE

#define OE_ON
#define OE_OFF


#else

#define OE_ON gpio_bit_reset(GPIOB, GPIO_PIN_4)
#define OE_OFF gpio_bit_set(GPIOB, GPIO_PIN_4)
#endif

enum LED_ID {
    SPEED_1 = 0,
    SPEED_2,
    MODE,
    BAT_UP_1,
    BAT_UP_2,
    BAT_DOWN_1,
    BAT_DOWN_2,
    RANGE_0,
    RANGE_1,
    RANGE_2,
    RANGE_3,
    ODO_0,
    ODO_1,
    ODO_2,
    ODO_3,
    ODO_4,
    ODO_5,
    LED_END,
};
void show_7led(uint8_t led_id, uint8_t n);

void sel_vcc(uint32_t i);
void spi0_send(uint16_t data);



//void km_led(uint32_t num, uint32_t n);



#ifdef __cplusplus
}
#endif

#ifdef __cplusplus
class led
{

public:
    led();
    ~led();

};
#endif



#endif
